﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ZBJF.Warranty.BLL.Cached;
using ZBJF.Warranty.Common.Common;

namespace ZBJF.Warranty.TheThirdPartyInteraction.Sellers.ChiDu
{
    public class ChiDuDataInteraction
    {
        private string chiDukey = "!!zbjfapi888@@";
        private string checkResultUrl = "http://api2018.sjgl580.com/cdapi/zbjf";
        private string huaAnKey = "zbjf_ha666888@!#";

        public ChiDuDataInteraction(string chiDukey, string checkResultUrl= "http://api2018.sjgl580.com/cdapi/zbjf")
        {
            this.chiDukey = chiDukey;
            this.checkResultUrl = checkResultUrl;
            this.huaAnKey = AppConfigCached.GetAppConfig().HuaAnSecretKey;
        }

        #region 调用尺度审核通知结果接口 + async Task<bool> InvokeChiduCheckResultInterface(string thirdUniqueNumber, bool result, string remark)
        /// <summary>
        /// 调用尺度审核通知结果接口
        /// </summary>
        /// <param name="thirdUniqueNumber"></param>
        /// <returns></returns>
        public async Task<bool> InvokeChiduCheckResultInterface(string thirdUniqueNumber, bool result, string remark)
        {
            Task<bool> re = Task.Run<bool>(() =>
            {
                bool chiduResult = this.SendCheckResult(thirdUniqueNumber, result, remark);
                if (!chiduResult)
                {
                    chiduResult = this.SendCheckResult(thirdUniqueNumber, result, remark);
                }
                return chiduResult;
            });
            await re;
            return re.Result;
        }
        #endregion

        #region 审核结果通知 + bool SendCheckResult(string orderId, bool result,string remark)
        /// <summary>
        /// 审核结果通知
        /// 尺度审核通知结果返回格式：
        ///成功：ok
        ///失败： error:原因
        /// </summary>
        /// <param name="orderId">订单号</param>
        /// <param name="Result">审核结果</param>
        /// <param name="remark">审核不通过原因</param>
        /// <returns></returns>
        private bool SendCheckResult(string orderId, bool result, string remark)
        {
            WebUtils web = new WebUtils();
            IDictionary<string, string> param1 = new Dictionary<string, string>();
            string str = GetCheckResult( orderId,  result? ChiDuResultEnum.pass:ChiDuResultEnum.nopass,  remark);
            param1.Add("rcode", str);
            string r = web.DoGet(checkResultUrl, param1);
            if (r.IndexOf("ok") == 0)
            {
                return true;
            }
            else
            {
                LogHelper.WriteLog("尺度审核结果通知返回错误信息：" + r + "；\r\n 请求内容：" + str, LogType.remark);
                return false;
            }
        }
        #endregion

        #region 生成审核通知消息密文 + string GetCheckResult(string orderId, ChiDuResultEnum result, string remark)
        /// <summary>
        /// 生成审核通知消息密文
        /// </summary>
        /// <param name="orderId">订单号</param>
        /// <param name="Result">审核结果（true:通过；false:不通过；null:未审核）</param>
        /// <param name="remark">审核不通过原因</param>
        /// <returns></returns>
        public string GetCheckResult(string orderId, ChiDuResultEnum result, string remark)
        {
            long timestamp = CommonMethod.GetTimeStamp(DateTime.Now);
            string huaAnResult = "",chiDuResult= EnumHelper.Instance.GetDescription(typeof(ChiDuResultEnum), result);

            if (result == ChiDuResultEnum.pass)
                huaAnResult = "1";
            else
                huaAnResult = "0";

            //华安密文
            string temp = string.Format("{0}|{1}|{2}", orderId, huaAnResult, timestamp);
            string huaAnMessage = new ChiDuAesHelper(huaAnKey).Encrypt(temp);

            string str = string.Format("{0}|{1}|{2}|{3}", orderId, chiDuResult, huaAnMessage, timestamp);
            return new ChiDuAesHelper(chiDukey).Encrypt(str);
        }
        #endregion
    }
}
